/***********************************************************************

	This file is part of KEEL-software, the Data Mining tool for regression, 
	classification, clustering, pattern mining and so on.

	Copyright (C) 2004-2010
	
	F. Herrera (herrera@decsai.ugr.es)
    L. Sánchez (luciano@uniovi.es)
    J. Alcalá-Fdez (jalcala@decsai.ugr.es)
    S. García (sglopez@ujaen.es)
    A. Fernández (alberto.fernandez@ujaen.es)
    J. Luengo (julianlm@decsai.ugr.es)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see http://www.gnu.org/licenses/
  
**********************************************************************/

/**
 * <p>
 * @author Written by Pedro Antonio GutiÃ©rrez and Juan Carlos FernÃ¡ndez (University of CÃ³rdoba) 23/10/2008
 * @version 1.0
 * @since JDK1.5
 * </p>
 */
package keel.GraphInterKeel.datacf.partitionData;

import javax.swing.JOptionPane;

public class HoldOutOptionsJDialog extends javax.swing.JDialog {

    /**
     * <p>
     * Option dialog for Hold Out partition type
     * </p>
     */
    
    /** 
     * <p>
     * Constructor that initializes the dialog
     * </p>
     */
    public HoldOutOptionsJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
        //this.setResizable(false);
    }

    /**
     * <p>
     * This method is called from within the constructor to
     * initialize the form.
     * </p>
     * <p>
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     * </p>
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        holdOutjPanel = new javax.swing.JPanel();
        this.setResizable(false);
        partitionsjLabel = new javax.swing.JLabel();
        percentagesjLabel = new javax.swing.JLabel();
        holdOutjSpinner = new javax.swing.JSpinner();
        holdOutjComboBox = new javax.swing.JComboBox();
        buttonsjPanel = new javax.swing.JPanel();
        acceptjButton = new javax.swing.JButton();
        canceljButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        holdOutjPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Hold-Out"));
        holdOutjPanel.setName("holdOutjPanel"); // NOI18N

        partitionsjLabel.setText("Number of Partitions");
        partitionsjLabel.setName("partitionsjLabel"); // NOI18N

        percentagesjLabel.setText("Percentages");
        percentagesjLabel.setName("percentagesjLabel"); // NOI18N

        holdOutjSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        holdOutjSpinner.setToolTipText("Number of Training/Test Pairs to be Generated");
        holdOutjSpinner.setName("holdOutjSpinner"); // NOI18N

        holdOutjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "50% Training - 50% Test", "66% Training - 33% Test", "75% Training - 25% Test", "80% Training - 20% Test", "84% Training - 16% Test", "86% Training - 14% Test", "88% Training - 12% Test", "89% Training - 11% Test", "90% Training - 10% Test" }));
        holdOutjComboBox.setToolTipText("Training/Test Percentages");
        holdOutjComboBox.setName("holdOutjComboBox"); // NOI18N

        javax.swing.GroupLayout holdOutjPanelLayout = new javax.swing.GroupLayout(holdOutjPanel);
        holdOutjPanel.setLayout(holdOutjPanelLayout);
        holdOutjPanelLayout.setHorizontalGroup(
            holdOutjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, holdOutjPanelLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(holdOutjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, holdOutjPanelLayout.createSequentialGroup()
                        .addComponent(percentagesjLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 93, Short.MAX_VALUE)
                        .addComponent(holdOutjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, holdOutjPanelLayout.createSequentialGroup()
                        .addComponent(partitionsjLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 141, Short.MAX_VALUE)
                        .addComponent(holdOutjSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(20, 20, 20))
        );
        holdOutjPanelLayout.setVerticalGroup(
            holdOutjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(holdOutjPanelLayout.createSequentialGroup()
                .addGroup(holdOutjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(partitionsjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(holdOutjSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(holdOutjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(percentagesjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(holdOutjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonsjPanel.setName("buttonsjPanel"); // NOI18N
        buttonsjPanel.setLayout(new java.awt.GridBagLayout());

        acceptjButton.setText("Accept");
        acceptjButton.setToolTipText("Accept the Current Values");
        acceptjButton.setName("acceptjButton"); // NOI18N
        acceptjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptjButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 124);
        buttonsjPanel.add(acceptjButton, gridBagConstraints);

        canceljButton.setText("Cancel");
        canceljButton.setToolTipText("Discard the Current Values");
        canceljButton.setName("canceljButton"); // NOI18N
        canceljButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canceljButtonActionPerformed(evt);
            }
        });
        buttonsjPanel.add(canceljButton, new java.awt.GridBagConstraints());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(holdOutjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(buttonsjPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(holdOutjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonsjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void acceptjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptjButtonActionPerformed
    try {
        nOfPartitions = (Integer) holdOutjSpinner.getValue();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this,
                "Insert an integer number for the number of partitions, please" + ex.getLocalizedMessage(), "Error", 2);
        nOfPartitions = 1;
        return;
    }
    if (nOfPartitions <= 0) {
        JOptionPane.showMessageDialog(this,
                "Insert a positive number or the number of partitions, please", "Error", 2);
        nOfPartitions = 1;
        return;
    }
    totalFractions = holdOutjComboBox.getSelectedIndex() + 2;

    setVisible(false);
}//GEN-LAST:event_acceptjButtonActionPerformed

private void canceljButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canceljButtonActionPerformed
    setVisible(false);
    holdOutjSpinner.setValue(nOfPartitions);
    holdOutjComboBox.setSelectedIndex(totalFractions - 2);
}//GEN-LAST:event_canceljButtonActionPerformed

    /**
     * Main method
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                HoldOutOptionsJDialog dialog = new HoldOutOptionsJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptjButton;
    private javax.swing.JPanel buttonsjPanel;
    private javax.swing.JButton canceljButton;
    private javax.swing.JComboBox holdOutjComboBox;
    private javax.swing.JPanel holdOutjPanel;
    private javax.swing.JSpinner holdOutjSpinner;
    private javax.swing.JLabel partitionsjLabel;
    private javax.swing.JLabel percentagesjLabel;
    // End of variables declaration//GEN-END:variables
    
    /** Number of partitions */
    protected int nOfPartitions = 1;

    /** Number of fractions */
    protected int totalFractions = 2;

    /**
     * <p>
     * Gets the number of fractions of the hold-out process. Two fractions
     * correspond to a "50%Training/50%Testing" configuration, three fractions
     * to  "66%Training/33%Testing", four fractions to
     * "75%Training/25%Testing"...
     * </p>
     * @return int Number of fractions
     */
    public int getTotalFractions() {
        return totalFractions;
    }

    /**
     * <p>
     * Gets the number of partitions to be performed, i.e. the number of times
     * that a random division using the configuration selected is going
     * to be performed.
     * </p>
     * @return int Number of partitions
     */
    public int getNOfPartitions() {
        return nOfPartitions;
    }
}

